package com.pokermadness.game;

import java.util.ArrayList;
import java.util.Random;

import ca.ualberta.cs.poker.Hand;

public class Player {
	public Player(int player_no, GameState game, ArrayList<LogMessage> log) {
		this.game = game;
		this.log = log;
		no = player_no;
		initPlayer();
	}

	public void initPlayer() {
		stack = 100;
		bet = 0;
		totalBet = 0;
		active = true;
		acted = false;
	}

	public GameState game;
	public int no;
	public int stack;
	public int bet;
	public int totalBet;
	public boolean active;
	public boolean acted;
	public Hand hand;
	private ArrayList<LogMessage> log;

	public void fold() {
		this.acted = true;
		this.active = false;
		log.add(new LogMessage(" folded.", this.no, LogMessage.MOVE_INFO));
	}

	public void call() {
		this.acted = true;
		this.decreaseStack(game.highestBet - this.bet);
		if (this.bet > 0)
			log.add(new LogMessage(" called €" + this.bet + ".", this.no,
					LogMessage.MOVE_INFO));
		else
			log.add(new LogMessage(" checked.", this.no,
					LogMessage.MOVE_INFO));
	}

	public void raise() {
		this.acted = true;
		game.highestBet = game.highestBet + game.raiseBy;
		this.decreaseStack(game.highestBet - this.bet);
		game.raiseLimit--;
		if (this.bet == game.raiseBy)
			log.add(new LogMessage(" bet €" + this.bet + ".", this.no,
					LogMessage.MOVE_INFO));
		else
			log.add(new LogMessage(" raised €" + this.bet + ".", this.no,
					LogMessage.MOVE_INFO));
	}

	public void decreaseStack(int amount) {
		if (amount >= this.stack) {
			amount = this.stack;
		}
		this.stack = this.stack - amount;
		this.bet = this.bet + amount;
		this.totalBet = this.totalBet + amount;
	}

	public void increaseStack(int amount) {
		this.stack = this.stack + amount;
	}

	public boolean canRaise() {
		if (game.raiseLimit > 0
				&& this.stack >= game.highestBet + game.raiseBy - this.bet) {
			return true;
		}
		return false;
	}

	public int makeDecision() {
		if (!this.active)
			return -1;
		Random rnd = new Random();
		float rand = rnd.nextFloat();
		if (rand < 0.2) {
			this.fold();
			return 0;
		} else if (rand >= 0.2 && rand <= 0.8) {
			this.call();
			return 1;
		} else {
			if (canRaise()) {
				this.raise();
				return 2;
			} else {
				this.call();
				return 1;
			}
		}
	}
}
